<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标准号" prop="a100">
        <el-input
          v-model="queryParams.a100"
          placeholder="请输入标准号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准名称" prop="a298">
        <el-input
          v-model="queryParams.a298"
          placeholder="请输入标准名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准类型" prop="a850">
        <el-input
          v-model="queryParams.a850"
          placeholder="请输入标准类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入分类名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="unitName">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册地址" prop="cra">
        <el-input
          v-model="queryParams.cra"
          placeholder="请输入注册地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布日期" prop="a101">
        <el-date-picker clearable
          v-model="queryParams.a101"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="标准状态" prop="a200">
        <el-input
          v-model="queryParams.a200"
          placeholder="请输入标准状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人" prop="legalperson">
        <el-input
          v-model="queryParams.legalperson"
          placeholder="请输入法人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否参与过抽取" prop="isRandom">
        <el-radio v-model="queryParams.isRandom" label="0">否</el-radio>
        <el-radio v-model="queryParams.isRandom" label="1">是</el-radio>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:standard:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:standard:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:standard:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:standard:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImportant"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-eleme"
          size="mini"
          @click="randomImportant"
        >随机抽取</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="standardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标准号" align="center" prop="a100" />
      <el-table-column label="标准名称" align="center" prop="a298" />
      <el-table-column label="标准类型" align="center" prop="a850" />
      <el-table-column label="分类名" align="center" prop="categoryName" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="发布日期" align="center" prop="a101" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.a101, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标准状态" align="center" prop="a200" >
        <template slot-scope="scope">
          <span class="statusyx" v-if="scope.row.a200=='现行有效'">{{scope.row.a200}}</span>
          <span class="statusfz" v-if="scope.row.a200=='企业自行废止'">{{scope.row.a200}}</span>
        </template>
      </el-table-column>
      <el-table-column label="法人" align="center" prop="legalperson" />
      <el-table-column label="电子文本" align="center" prop="a893" >
        <template slot-scope="scope">
          <a class="txtDown" v-if="scope.row.a893!=null" :href="scope.row.a893" target="_blank" ><el-tag>电子文本</el-tag></a>
        </template>
      </el-table-column>
      <el-table-column label="是否参与抽取" align="center" prop="isRandom" >
        <template slot-scope="scope">
          <span v-if="scope.row.isRandom=='0'">否</span>
          <span v-if="scope.row.isRandom=='1'">是</span>
        </template>
      </el-table-column>
      <el-table-column label="问题" align="center" prop="problem" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.problem ==null"
            icon="el-icon-edit"
            @click="handleProblem(scope.row)"
            v-hasPermi="['system:standard:edit']"
          >录入问题</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:standard:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:standard:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改双随机标准对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标准号" prop="a100">
          <el-input v-model="form.a100" placeholder="请输入标准号" />
        </el-form-item>
        <el-form-item label="标准名称" prop="a298">
          <el-input v-model="form.a298" placeholder="请输入标准名称" />
        </el-form-item>
        <el-form-item label="标准类型" prop="a850">
          <el-input v-model="form.a850" placeholder="请输入标准类型" />
        </el-form-item>
        <el-form-item label="分类名" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="uscc">
          <el-input v-model="form.uscc" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="单位名称" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="行政区划" prop="adivision">
          <el-input v-model="form.adivision" placeholder="请输入行政区划" />
        </el-form-item>
        <el-form-item label="注册地址" prop="cra">
          <el-input v-model="form.cra" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="发布日期" prop="a101">
          <el-date-picker clearable
            v-model="form.a101"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="标准状态" prop="a200">
          <el-input v-model="form.a200" placeholder="请输入标准状态" />
        </el-form-item>
        <el-form-item label="法人" prop="legalperson">
          <el-input v-model="form.legalperson" placeholder="请输入法人" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="电子文本地址" prop="a893">
          <el-input v-model="form.a893" placeholder="请输入电子文本地址" />
        </el-form-item>
        <el-form-item label="是否参与" prop="isRandom">
          <el-input v-model="form.isRandom" placeholder="请输入是否参与" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新日期" prop="updateDate">
          <el-date-picker clearable
            v-model="form.updateDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 分类 文件 -->
    <el-dialog v-loading="loadingimport" :title="title" :visible.sync="openImportant" width="800px" append-to-body>
      <el-form ref="form" label-width="80px">
        <el-row>
          <el-col :span="9">
            <el-form-item label="标准分类" >
              <el-tree
                :data="treedata"
                show-checkbox
                default-expand-all
                node-key="id"
                check-strictly="true"
                @check="getname"
                ref="tree"
                highlight-current
                :props="defaultProps">
              </el-tree>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标准文件" >
              <el-upload
                class="upload-demo"
                drag
                :on-success="uploadSucess"
                action="/dev-api/system/standard/upload"
                multiple>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitImportantForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 随机抽取 -->
    <el-dialog v-loading="loadingrandom" :title="title" :visible.sync="openRandom" width="1000px" append-to-body>
      <el-form ref="form" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标准分类">
              <el-tree
                :data="treedata"
                show-checkbox
                default-expand-all
                node-key="id"
                check-strictly="true"
                @check="getname"
                ref="tree"
                highlight-current
                :props="defaultProps">
              </el-tree>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="是否已抽取" label-width="100px;">
               <el-radio v-model="isaRandom" label="0">否</el-radio>
                <el-radio v-model="isaRandom" label="1">是</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="标准发布时间" label-width="100px;">
              <el-date-picker clearable
                v-model="randoma101"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择创建日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="抽取数量" label-width="100px;">
              <el-input-number v-model="randomNum" @change="handleRandomChange" :min="1"  label="抽取数量"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleRandomExport"
        >导出</el-button>
      </el-col>
      <el-table :data="standardrandomList" >
      <el-table-column label="标准号" align="center" prop="a100" />
      <el-table-column label="标准名称" align="center" prop="a298" />
      <el-table-column label="标准类型" align="center" prop="a850" />
      <el-table-column label="分类名" align="center" prop="categoryName" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="发布日期" align="center" prop="a101" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.a101, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标准状态" align="center" prop="a200" >
        <template slot-scope="scope">
          <span class="statusyx" v-if="scope.row.a200=='现行有效'">{{scope.row.a200}}</span>
          <span class="statusfz" v-if="scope.row.a200=='企业自行废止'">{{scope.row.a200}}</span>
        </template>
      </el-table-column>
      <el-table-column label="法人" align="center" prop="legalperson" />
      <el-table-column label="电子文本" align="center" prop="a893" >
        <template slot-scope="scope">
          <a class="txtDown" v-if="scope.row.a893!=null" :href="scope.row.a893" target="_blank" ><el-tag>电子文本</el-tag></a>
        </template>
      </el-table-column>
      <el-table-column label="是否参与抽取" align="center" prop="isRandom" >
        <template slot-scope="scope">
          <span v-if="scope.row.isRandom=='0'">否</span>
          <span v-if="scope.row.isRandom=='1'">是</span>
        </template>
      </el-table-column>
      <el-table-column label="问题" align="center" prop="remark" />

    </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRandomForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 录入问题 -->
    <el-dialog v-loading="loadingProblem" :title="title" :visible.sync="openProblem" width="800px" append-to-body>
      <el-form ref="form" label-width="80px">
        <el-row>
          <el-col :span="19">
            <el-form-item label="问题" >
              <el-input
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 8}"
                placeholder="请输入内容"
                v-model="standardProblem">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProblemForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStandard, getStandard, delStandard, addStandard, updateStandard, importStandard, randomStandard, randomStandardDown } from "@/api/system/standard";
import { listCategory } from "@/api/system/category";
import { addProblem } from "@/api/system/problem";

export default {
  name: "Standard",
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingimport: false,
      loadingrandom: false,
      loadingProblem: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 双随机标准表格数据
      standardList: [],
      //抽取表格数据
      standardrandomList: [],
      // 文件路径
      filePath : '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openImportant: false,
      openRandom: false,
      openProblem: false,
      // 发布时间
      a101: "",
      //树型类型数组
      treedata: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        identifying: null,
        a100: null,
        a298: null,
        a850: null,
        categoryName: null,
        uscc: null,
        unitName: null,
        phone: null,
        adivision: null,
        cra: null,
        a101: null,
        a200: null,
        legalperson: null,
        contacts: null,
        a893: null,
        status: null,
        isRandom: null,
        createDate: null,
        updateDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        identifying: [
          { required: true, message: "唯一标识不能为空", trigger: "blur" }
        ],
        a100: [
          { required: true, message: "标准号不能为空", trigger: "blur" }
        ],
        a298: [
          { required: true, message: "标准名称不能为空", trigger: "blur" }
        ],
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      //是否参与过抽取
      isaRandom:'0',
      //随机数量
      randomNum:0,
      //随机发布时间
      randoma101:null,
      //标准问题
      standardProblem:'',
      problemParam:{
        standIden:null,
        a298:null,
        a100:null,
        problem:null
      }
    };
  },
  created() {
    this.getList();
    listCategory().then(response => {
      this.treedata = this.handleTree(response.data, "id", "parentid");
    });
  },
  methods: {
    /** 查询双随机标准列表 */
    getList() {
      this.loading = true;
      listStandard(this.queryParams).then(response => {
        this.standardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openImportant = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        identifying: null,
        a100: null,
        a298: null,
        a850: null,
        categoryName: null,
        uscc: null,
        unitName: null,
        phone: null,
        adivision: null,
        cra: null,
        a101: null,
        a200: null,
        legalperson: null,
        contacts: null,
        a893: null,
        status: null,
        isRandom: null,
        remark: null,
        createDate: null,
        updateDate: null
      };
      this.importForm = {
        category:null,
        filePath:null
      }
      this.resetForm("form");
      this.resetForm("formImportant");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加双随机标准";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStandard(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改双随机标准";
      });
    },
    /** 提出问题 */
    handleProblem(row){
      this.openProblem = true;
      // 问题框
      this.reset();
      this.standardProblem = row.problem;
      this.problemParam.standIden = row.identifying;
      this.problemParam.a298 = row.a298;
      this.problemParam.a100 = row.a100;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStandard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStandard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
     /** 导入按钮 */
    submitImportantForm() {
      this.loadingimport = true;
      //树型数据
      let chosetree = this.$refs.tree.getCheckedNodes();
      let formData = new FormData();
      formData.append("filePath",this.filePath);
      formData.append("category",chosetree[0].name);
      //文件路径
      importStandard(formData).then(response => {
        this.loadingimport = false;
        let data = response.data;
        this.openImportant = false;
        if(response.code==200){
          this.$message.success("导入成功!"+"共用时"+data.totalTime+"秒共导入"+data.totalNum+"条 新增："+data.adNum+"条更新："+data.upNum);
        }else{
          this.$message.error(response.msg);
        }
      })
    },
    /**问题按钮 */
    submitProblemForm(){
      this.problemParam.problem = this.standardProblem;
      addProblem(this.problemParam).then(response=>{
        if(response.code==200){
          this.$message.success(response.msg);
        }
        this.openProblem = false;
      })
    },
    /** 随机按钮 */
    submitRandomForm(){
      this.loadingrandom = true;
      //树型数据
      let chosetree = this.$refs.tree.getCheckedNodes();
      let treename = '';
      if(chosetree.length!=0){
        treename= chosetree[0].name;
      }
      let formData = new FormData();
      formData.append("categoryName",treename);
      formData.append("isaRandom",this.isaRandom);
      formData.append("randomDate",this.randoma101);
      formData.append("randomNum",this.randomNum);
      randomStandard(formData).then(response=>{
        this.loadingrandom = false;
        this.standardrandomList = response.data;
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除标准编号为"' + ids + '"的数据项？').then(function() {
        return delStandard(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/standard/export', {
        ...this.queryParams
      }, `standard_${new Date().getTime()}.xlsx`)
    },
    /** 导出随机内容 */
    handleRandomExport(){
      //树型数据
      let chosetree = this.$refs.tree.getCheckedNodes();
      let treename = '';
      if(chosetree.length!=0){
        treename= chosetree[0].name;
      }
      let formparams = {
        categoryName:treename,
        isaRandom: this.isaRandom,
        randomDate: this.randoma101,
        randomNum: this.randomNum
      }
      this.download('system/standard/randomStandardDown', {
        ...formparams
      }, `standard_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImportant() {
      this.reset();
      this.openImportant = true;
      this.title = "标准导入";
    },
    /** */
    randomImportant() {
      this.reset();
      this.openRandom = true;
      this.title = "标准抽取";
    },
    /*文件上传成功*/
    uploadSucess(response, file, fileList){
      this.filePath = file.response;
    },
    /**树型菜单只能选一个判断 */
    // 选择会触发getname方法
    getname() {
      // 获取当节点的值
      var getlist = this.$refs.tree.getCheckedNodes().concat(this.$refs.tree.getHalfCheckedNodes());
      // 循环遍历当前节点的值
      for (var i in getlist) {
        // 判断子节点是否存在子节点 如果存在直接请求并且提示 false
        if(getlist.length == 1) {
        }else {
          this.$message.error("只选择一个节点");
          this.$refs.tree.setCheckedKeys([]);
          return
        }
      }
    },
    //抽取数量
    handleRandomChange(value){
      this.randomNum = value;
    }
  }
};
</script>
<style scoped lang="scss">
.txtDown{
  color:#fff;
  background-color: #1b66e6;
}

.statusyx{
  color:green;
  font-weight: bold;
}

.statusfz{
   color:red;
   font-weight: bold;
}
</style>
